﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using lauimini_webform.BLL;
using lauimini_webform.Data;
using mydesign.Models;
namespace lauimini_webform
{
    public partial class achievement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MyDBContext dB = new MyDBContext();
            UserService userService = new UserService();
            List<Accomplishment> accomplishments = dB.Accomplishment.ToList();
            if (Session["userAccount"] == null)
            {
                Response.Redirect("~/login.aspx");
            }
            string userAccount = Session["userAccount"].ToString();
            int userid = userService.getUserid(userAccount);
            List<MyComplish> myComplishes = dB.MyComplish.Where(x => x.usrid == userid).ToList();
            foreach (var my in myComplishes)
            {
                for (int i = 0; i < accomplishments.Count; i++)
                {
                    if (myComplishes.Count <= 0)
                    {
                        break;
                    }
                    if (accomplishments[i].description.Contains(my.description))
                    {
                        accomplishments.Remove(accomplishments[i]);
                    }
                }
            }
            if (accomplishments.Count == 0)
            {
                getIt.Visible = false;
                Response.Write("你已领取了所有的成就");
            }


            Repeater1.DataSource = accomplishments;
            Repeater1.DataBind();
        }

        protected void getIt_Click(object sender, EventArgs e)
        {
            MyDBContext dB = new MyDBContext();
            UserService userService = new UserService();
            UserTargetService userTargetService = new UserTargetService();
            string userAccount = Session["userAccount"].ToString();
            int userid = userService.getUserid(userAccount);

            int count = userTargetService.getFinishMyTarget(userid).Count;
            if (count == 0)
            {
                Response.Write("<script>alert('未达成领取条件')</script>");
            }


            switch (count)
            {
                case 0:

                    break;
                case 1:
                    AddMyComplish("黑铁", "完成一个任务");
                    break;
                case 2:
                    AddMyComplish("青铜", "完成两个任务");
                    break;
                case 3:
                    AddMyComplish("白银", "完成三个任务");
                    break;
                case 4:
                    AddMyComplish("铂金", "完成四个任务");
                    break;
                case 5:
                    AddMyComplish("水晶", "完成五个任务");
                    break;
                case 6:
                    AddMyComplish("星耀", "完成六个任务");
                    break;
                case 7:
                    AddMyComplish("王者", "完成七个任务");
                    break;
                default:
                    break;
            }

        }

        protected void AddMyComplish(string description, string detail)
        {
            MyDBContext dB = new MyDBContext();
            UserService userService = new UserService();
            UserTargetService userTargetService = new UserTargetService();
            string userAccount = Session["userAccount"].ToString();
            int userid = userService.getUserid(userAccount);

            List<MyComplish> myComplishes1 = dB.MyComplish.ToList();

            foreach (var item in myComplishes1)
            {
                if (description.Contains(item.description))
                {
                    return;
                }
            }

            MyComplish myComplish = new MyComplish();
            myComplish.usrid = userid;
            myComplish.description = description;
            myComplish.detail = detail;
            dB.MyComplish.Add(myComplish);
            dB.SaveChanges();


            List<Accomplishment> accomplishments = dB.Accomplishment.ToList();
            List<MyComplish> myComplishes = dB.MyComplish.Where(x => x.usrid == userid).ToList();
            foreach (var my in myComplishes)
            {
                for (int i = 0; i < accomplishments.Count; i++)
                {
                    if (myComplishes.Count <= 0)
                    {
                        break;
                    }
                    if (accomplishments[i].description.Contains(my.description))
                    {
                        accomplishments.Remove(accomplishments[i]);
                    }
                }
            }
            if (accomplishments.Count == 0)
            {
                getIt.Visible = false;
                Response.Write("你已领取了所有的成就");
            }


            Repeater1.DataSource = accomplishments;
            Repeater1.DataBind();
        }
    }
}
